/// <reference path="../../typings/index.d.ts" />
'use strict';
const express = require('express');
const router = express.Router();
const mongoose = require('mongoose');
const Task = mongoose.model('Task');

module.exports = {
    listHandler: (req, res, next) => {
        Task.find((findErr, tasks) => {
            if (findErr) {
                res.json({
                    err: { msg: '查找时出现错误！！', err: findErr },
                    data: null,
                    userinfo: {
                        username: req.session.username,
                        userpass: req.session.userpass
                    }
                })
            } else {
                res.json({
                    err: null,
                    data: tasks,
                    userinfo: {
                        username: req.session.username,
                        userpass: req.session.userpass
                    }
                })
            }
        })
    },
    findByStr: (req, res, next) => {
        Task.find(req.body, (findErr, tasks) => {
            if (findErr) {
                res.json({
                    err: { msg: '查找时出现错误！！', err: findErr },
                    data: null,
                    userinfo: {
                        username: req.session.username,
                        userpass: req.session.userpass
                    }
                })
            } else {
                res.json({
                    err: null,
                    data: tasks,
                    userinfo: {
                        username: req.session.username,
                        userpass: req.session.userpass
                    }
                })
            }
        })
    }
}